package com.iyundao.io.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iyundao.io.Service.ActivityService;
import com.iyundao.io.mapper.*;
import com.iyundao.io.model.*;
import com.iyundao.io.tools.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityModelMapper activityModelMapper;

    @Autowired
    private ActivityJoinPeopleMapper joinPeopleMapper;

    @Autowired
    private PartyMemberMapper partyMemberMapper;

    @Autowired
    private ActivityImageMapper imageMapper;

    @Autowired
    private SignInImageMapper signInImageMapper;

    @Autowired
    private ActivityModelLookBackMapper modelLookBackMapper;

    @Autowired
    private ActivityModelCustomMapper modelCustomMapper;

    @Autowired
    private SysDeptCustomMapper deptCustomMapper;

    @Autowired
    private ActivityModelLookBackCustomMapper lookBackCustomMapper;

    @Autowired
    private PartyMemberCustomMapper memberCustomMapper;

    @Override
    public ResponseData insert(ActivityModel activityModel) {
        if (activityModel.equals("") | activityModel == null) {
            return new ResponseData(0, "传入数据为空");
        } else {
            Date date = new Date();
            activityModel.setActivitycreatetime(date);
            activityModel.setActivityflag(0);
            int i = activityModelMapper.insert(activityModel);
            if (i > 0) {
                return new ResponseData(200, "活动添加成功");
            } else {
                return new ResponseData(0, "添加失败");
            }
        }
    }

    @Override
    public ResponseData selectById(Integer id) {
        //SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        ActivityModelVo model = modelCustomMapper.selectShowById(id);
        SysDeptProvider sysDeptProvider = deptCustomMapper.selectBySid(model.getOrganizationid());
        model.setReal_name(sysDeptProvider.getRealName());
        return new ResponseData(200,"成功",model);
    }

    /**
     * 根据组织id查询组织下所有的活动
     * @param pagenum
     *          页数
     * @param pagesize
     *          每页显示条数
     * @param organizationid
     *          组织id
     * @return
     */
    @Override
    public PageInfo<ActivityModelVo> selectAllByOrganizationId(Integer pagenum, Integer pagesize, String organizationid) {
        List<ActivityModelVo> list = modelCustomMapper.selectAllByOrganizationId(organizationid);
        List<SysDept> sysDepts = deptCustomMapper.selectByRoleId(organizationid);
        PageHelper.startPage(pagenum, pagesize);

        for (SysDept syeDept: sysDepts) {
            for (ActivityModelVo activityModelVo: list) {
                String sid = activityModelVo.getOrganizationid();
                if (syeDept.getSid().equals(sid)) {
                    activityModelVo.setReal_name(syeDept.getRealName());
                }
            }
        }

        PageInfo<ActivityModelVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 根据组织id，与状态查询活动，查询本组织下的已审核活动，未审核活动
     * @param pagenum
     *          页数
     * @param pagesize
     *          每页显示的条数
     * @param organizationid
     *          组织id
     * @param activityflag
     *          状态
     * @return
     */
    @Override
    public PageInfo<ActivityModelVo> findByOrganizationIdAndFlag(Integer pagenum, Integer pagesize, String organizationid, Integer activityflag) {
        List<ActivityModelVo> list = modelCustomMapper.findByOrganizationIdAndFlag(organizationid, activityflag);

        PageHelper.startPage(pagenum, pagesize);

        List<SysDept> sysDepts = deptCustomMapper.selectByRoleId(organizationid);
        for (SysDept syeDept: sysDepts) {
            for (ActivityModelVo activityModelVo: list) {
                String sid = activityModelVo.getOrganizationid();
                if (syeDept.getSid().equals(sid)) {
                    activityModelVo.setReal_name(syeDept.getRealName());
                }
            }
        }
        PageInfo<ActivityModelVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 审核功能，即修改活动数据的状态flag
     * @param activityflag
     *      活动状态
     * @param id
     *      活动id
     *
     * @return
     */
    @Override
    public ResponseData updateFlag(Integer activityflag, Integer id) {
        int i = modelCustomMapper.updateFlag(activityflag, id);
        if (i > 0) {
            return new ResponseData(200, "审核成功");
        } else {
            return new ResponseData(0, "修改失败");
        }
    }

    /**
     * 添加允许参与活动的人员
     * @param array
     *      人员id数组
     * @param activityid
     *      活动id
     * @return
     */
    @Override
    public ResponseData joinPeople(Integer[] array, Integer activityid) {
        try {
            for (Integer a: array) {
                ActivityJoinPeople activityJoinPeople = new ActivityJoinPeople();
                activityJoinPeople.setActivityid(activityid);
                activityJoinPeople.setPartymemberid(a);
                joinPeopleMapper.insertSelective(activityJoinPeople);
            }
            return new ResponseData(200,"成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData(0,e.getMessage());
        }
    }

    /**
     * 查询最后一条数据的id
     * @return
     */
    @Override
    public Integer selectLastId() {
        return modelCustomMapper.selectLastId();

    }

    /**
     * 查询该项目下允许参与的人员
     * @param activityid
     * 活动id
     * @return
     */
    @Override
    public List<PartymemberVo> selectAllJoinByActivityId(Integer activityid) {
        List<PartymemberVo> list = partyMemberMapper.selectAllJoinByActivityId(activityid);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date time = new Date();
        String format = simpleDateFormat.format(time);
        //String format = "2019-05-22";
        String date = format + " 00:00:00";
        String date1 = format + " 23:59:59";

        List<PartyMember> list1 = partyMemberMapper.selectSignIn(activityid, date, date1);

        for (PartymemberVo partymemberVo:list) {
            partymemberVo.setaBoolean(false);
            for (PartyMember partymember:list1) {
                if (partymemberVo.getId().equals(partymember.getId())) {
                    partymemberVo.setaBoolean(true);
                }
            }
        }
        return list;
    }

    /**
     * 搜索已通过审核活动
     * @param s
     *          搜索内容
     * @return
     */
    @Override
    public PageInfo<ActivityModelVo> selectByLike(Integer pagenum, Integer pagesize, String s) {
        PageHelper.startPage(pagenum,pagesize);
        String activitytitle = "%" + s + "%";
        List<ActivityModelVo> list = modelCustomMapper.selectByLike(activitytitle);

        List<SysDept> sysDepts = deptCustomMapper.selectAll();
        for (SysDept syeDept: sysDepts) {
            for (ActivityModelVo activityModelVo: list) {
                String sid = activityModelVo.getOrganizationid();
                if (syeDept.getSid().equals(sid)) {
                    activityModelVo.setReal_name(syeDept.getRealName());
                }
            }
        }
        
        PageInfo<ActivityModelVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 搜索本组织下的活动
     * @param pagenum
     * 页数
     * @param pagesize
     * 条数
     * @param s
     * 搜索内容
     * @return
     */
    @Override
    public PageInfo<ActivityModelVo> selectByLikeAndSid(Integer pagenum, Integer pagesize, String s) {
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        String sid = sysUser.getSysDept().getSid();
        PageHelper.startPage(pagenum,pagesize);
        String activitytitle = "%" + s + "%";
        List<ActivityModelVo> list = modelCustomMapper.selectByLikeAndOrganization(activitytitle,sid);

        List<SysDept> sysDepts = deptCustomMapper.selectByRoleId(sid);
        for (SysDept syeDept: sysDepts) {
            for (ActivityModelVo activityModelVo: list) {
                String id = activityModelVo.getOrganizationid();
                if (syeDept.getSid().equals(id)) {
                    activityModelVo.setReal_name(syeDept.getRealName());
                }
            }
        }

        PageInfo<ActivityModelVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 搜索本组织下的精彩回顾
     * @param pagenum
     * 页数
     * @param pagesize
     * 条数
     * @param s
     * 搜索内容
     * @return
     */
    @Override
    public PageInfo<ActivityModelVo> selectLookBackByLikeAndSid(Integer pagenum, Integer pagesize, String s) {
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        String sid = sysUser.getSysDept().getSid();
        PageHelper.startPage(pagenum,pagesize);
        String activitytitle = "%" + s + "%";
        List<ActivityModelVo> list = modelCustomMapper.selectLookBackByLikeAndOrganization(activitytitle,sid);

        List<SysDept> sysDepts = deptCustomMapper.selectByRoleId(sid);
        for (SysDept syeDept: sysDepts) {
            for (ActivityModelVo activityModelVo: list) {
                String id = activityModelVo.getOrganizationid();
                if (syeDept.getSid().equals(id)) {
                    activityModelVo.setReal_name(syeDept.getRealName());
                }
            }
        }

        PageInfo<ActivityModelVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 根据活动id查询活动内容，以及活动的所有图片
     * @param id
     * 活动id
     * @return
     */
    @Override
    public ResponseData selectAllImageById(Integer id) {
        try {
            ActitvityShow actitvityShow = modelCustomMapper.selectAllById(id);
            List<String> imgurl = new ArrayList<>();

            //查询活动的所有图片
            List<String> strings = null;
            strings = imageMapper.selectAllUrlByActivityId(id);
            if (strings != null && !strings.equals("")){
                for (String url: strings) {
                    imgurl.add(url);
                }
            }

            //查询签到的所有图片
            List<String> strings1 = null;
            strings1 = signInImageMapper.selectAllUrlByActivityId(id);
            if (strings1 != null && !strings1.equals("")) {
                for (String url : strings1) {
                    imgurl.add(url);
                }
            }
            actitvityShow.setImgurl(imgurl);
            return new ResponseData(200,"查询成功",actitvityShow);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData(0,e.getMessage());
        }

    }

    /**
     * 查询组织下的活动
     * @param pagenum
     * @param pagesize
     * @param sid
     * @return
     */
    @Override
    public PageInfo<ActivityModelVo> selectByUserId(Integer pagenum, Integer pagesize, String sid) {
        PageHelper.startPage(pagenum,pagesize);
        List<ActivityModelVo> activityModelVos = modelCustomMapper.selectUserId(sid);
        List<SysDept> sysDepts = deptCustomMapper.selectByRoleId(sid);
        for (SysDept syeDept: sysDepts) {
            for (ActivityModelVo activityModelVo: activityModelVos) {
                String organizationid = activityModelVo.getOrganizationid();
                if (syeDept.getSid().equals(organizationid)) {
                    activityModelVo.setReal_name(syeDept.getRealName());
                }
            }
        }
        PageInfo<ActivityModelVo> pageInfo = new PageInfo<>(activityModelVos);
        return pageInfo;
    }

    /**
     * 通过活动id查询活动相关信息
     * @param id
     * 活动id
     * @return
     */
    @Override
    public ResponseData selectShowById(Integer id) {
        try {
            ActivityModelVo activityModelVo = modelCustomMapper.selectShowById(id);
            SysDeptProvider sysDept = deptCustomMapper.selectBySid(activityModelVo.getOrganizationid());
            activityModelVo.setReal_name(sysDept.getRealName());
            List<PartymemberVo> list = partyMemberMapper.selectAllJoinByActivityId(id);
            activityModelVo.setList1(list);
            return new ResponseData(200,"成功",activityModelVo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData(0,e.getMessage());
        }
    }

    /**
     * 查询所有通过审核的活动
     * @param pagenum
     * @param pagesize
     * @return
     */
    @Override
    public PageInfo<ActivityModelVo> selectAll(Integer pagenum, Integer pagesize) {
        PageHelper.startPage(pagenum,pagesize);
        List<ActivityModelVo> list = modelCustomMapper.selectAll();
        List<SysDept> list1 = deptCustomMapper.selectAll();
        for (SysDept sysdept: list1) {
            for (ActivityModelVo a: list) {
                if (a.getOrganizationid().equals(sysdept.getSid())) {
                    a.setReal_name(sysdept.getRealName());
                }
            }
        }
        PageInfo<ActivityModelVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 以组织id查询通过的活动
     * @param pagenum
     * @param pagesize
     * @param organizationid
     * @return
     */
    @Override
    public PageInfo<ActivityModelVo> selectAllBy(Integer pagenum, Integer pagesize, String organizationid) {
        PageHelper.startPage(pagenum,pagesize);
        List<ActivityModelVo> list = modelCustomMapper.selectAllBy(organizationid);
        for (ActivityModelVo a: list) {
            SysDeptProvider sysDept = deptCustomMapper.selectBySid(a.getOrganizationid());
            a.setReal_name(sysDept.getRealName());
        }
        PageInfo<ActivityModelVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 查询所有精彩回顾
     * @param pagenum
     * @param pagesize
     * @return
     */
    @Override
    public PageInfo<ActivityModelVo> selectAllLookBack(Integer pagenum, Integer pagesize) {
        PageHelper.startPage(pagenum,pagesize);
        List<ActivityModelVo> list = modelLookBackMapper.selectAll();

        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        String sid = sysUser.getSysDept().getSid();
        List<SysDept> sysDepts = deptCustomMapper.selectByRoleId(sid);

        for (SysDept syeDept: sysDepts) {
            for (ActivityModelVo activityModelVo: list) {
                String organizationid = activityModelVo.getOrganizationid();
                if (syeDept.getSid().equals(organizationid)) {
                    activityModelVo.setReal_name(syeDept.getRealName());
                }
            }
        }
        PageInfo<ActivityModelVo> pageInfo = new PageInfo<>(list);
        return pageInfo;

    }

    @Override
    public PageInfo<ActivityModelVo> selectAllLookBackBySid(Integer pagenum, Integer pagesize) {
        PageHelper.startPage(pagenum,pagesize);
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        List<ActivityModelVo> list = modelLookBackMapper.selectAllBySid(sysUser.getSysDept().getSid());
        String sid = sysUser.getSysDept().getSid();
        List<SysDept> sysDepts = deptCustomMapper.selectByRoleId(sid);

        for (SysDept syeDept: sysDepts) {
            for (ActivityModelVo activityModelVo: list) {
                String organizationid = activityModelVo.getOrganizationid();
                if (syeDept.getSid().equals(organizationid)) {
                    activityModelVo.setReal_name(syeDept.getRealName());
                }
            }
        }
        PageInfo<ActivityModelVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 添加精彩回顾
     * @param activityModelLookBack
     * @return
     */
    @Override
    public ResponseData insertLookBack(ActivityModelLookBack activityModelLookBack) {
        if (activityModelLookBack.toString().equals("") || activityModelLookBack.toString() == null) {
            return new ResponseData(0,"传入数据为空");
        } else {
            int i = modelLookBackMapper.insertSelective(activityModelLookBack);
            if (i > 0){
                return new ResponseData(200,"添加成功");
            } else {
                return new ResponseData(0,"添加失败");
            }
        }
    }

    /**
     * 下架精彩回顾
     * @param id
     * @return
     */
    @Override
    public ResponseData updateLookBackFlag(Integer id) {
        int i = modelLookBackMapper.updateFlag(id);
        if (i > 0){
            return new ResponseData(200,"成功");
        } else {
            return new ResponseData(0,"失败");
        }
    }

    /**
     *
     * @param activityModel
     * @return
     */
    @Override
    public ResponseData updateActivity(ActivityModel activityModel) {
        if (activityModel.equals("") | activityModel == null) {
            return new ResponseData(0, "传入数据为空");
        } else {
            activityModel.setActivityflag(0);
            int i = modelCustomMapper.check(activityModel);
            if (i > 0) {
                return new ResponseData(200, "审核成功");
            } else {
                return new ResponseData(0, "添加失败");
            }
        }
    }

    @Override
    public ResponseData updatePeople(Integer[] array, Integer activityid) {
        try {
            int i = joinPeopleMapper.del(activityid);
            /*if (i > 0) {*/
                for (Integer a: array) {
                    ActivityJoinPeople activityJoinPeople = new ActivityJoinPeople();
                    activityJoinPeople.setActivityid(activityid);
                    activityJoinPeople.setPartymemberid(a);
                    joinPeopleMapper.insertSelective(activityJoinPeople);
                }
                return new ResponseData(200,"成功");
           /* } else {
                return new ResponseData(0,"失败");
            }*/
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData(0,e.getMessage());
        }
    }

    /**
     * 修改精彩回顾
     * @param activityModel
     * @return
     */
    @Override
    public ResponseData updateActivityLookBack(ActivityModelLookBack activityModel) {
        if (activityModel.toString().equals("") || activityModel.toString() == null){
            return new ResponseData(0,"传入数据为空");
        } else {
            int i = lookBackCustomMapper.update(activityModel);
            if (i > 0) {
                return new ResponseData(200,"成功");
            } else {
                return new ResponseData(0,"失败");
            }
        }
    }

    /**
     * 根据id查询精彩回顾
     * @param id
     * @return
     */
    @Override
    public ResponseData selectLookBackById(Integer id) {
        ActivityModelVo activityModelLookBack = lookBackCustomMapper.selectLookBackById(id);
        return new ResponseData(200,"成功",activityModelLookBack);
    }

    /**
     * 党员用户查询本组织下已通过活动
     * @param activityTitle
     * @return
     */
    @Override
    public PageInfo<ActivityModelVo> selectAllByLike(Integer pagenum, Integer pagesize, String activityTitle) {
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        String sid = sysUser.getSysDept().getSid();
        SysDeptProvider sysDept = deptCustomMapper.selectBySid(sid);

        PageHelper.startPage(pagenum,pagesize);
        if (activityTitle.equals("") || activityTitle == null) {
            List<ActivityModelVo> list = modelCustomMapper.selectAllBy(sid);
            for (ActivityModelVo avo: list) {
                avo.setReal_name(sysDept.getRealName());
            }
            PageInfo<ActivityModelVo> pageInfo = new PageInfo<>(list);
            return pageInfo;
        } else {
            String s = "%" + activityTitle + "%";
            List<ActivityModelVo> list = modelCustomMapper.selectAllByLike(sid, s);
            for (ActivityModelVo avo: list) {
                avo.setReal_name(sysDept.getRealName());
            }
            PageInfo<ActivityModelVo> pageInfo = new PageInfo<>(list);
            return pageInfo;
        }
    }

    /**
     * 手机端根据组织名或活动名查询所有的精彩回顾
     * @param pagenum
     * @param pagesize
     * @param activityTitle
     *      活动名或组织名
     * @return
     */
    @Override
    public PageInfo<ActivityModelVo> selectAllLooKBackByLike(Integer pagenum, Integer pagesize, String activityTitle) {

        String s = "%" + activityTitle + "%";
        List<ActivityModelVo> list = modelCustomMapper.selectLookBackByLike(s);

        PageHelper.startPage(pagenum,pagesize);
        PageInfo<ActivityModelVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public ResponseData deleteActivity(Integer id) {
         modelCustomMapper.delteActivityModel(id);
         modelCustomMapper.delteActivityImage(id);
         return new ResponseData(200,"成功");
    }

    @Override
    public ResponseData deleteActivityLookBack(Integer id) {
        lookBackCustomMapper.deleteActivityModelLookBack(id);
        return new ResponseData(200,"成功");
    }
}
